package com.zwj.utils;


import com.zwj.dto.Meta;
import com.zwj.dto.SysMenu;
import com.zwj.dto.UserMenu;
import com.zwj.entity.UserMenuEntity;

import java.util.ArrayList;
import java.util.List;

public class TreeUtils {

    public static List createMenuTree(List<SysMenu> data, String menuId) {
        if(data.size()==0){
            return null;
        }
        List<SysMenu> children = new ArrayList<>();
        for(SysMenu tree:data){
            if(tree.getParentId().equals(menuId)){
                List<SysMenu> childrenData = new ArrayList<>(data);
                childrenData.remove(tree);
                tree.setChildren(createMenuTree(childrenData,tree.getId()));
                children.add(tree);
            }
        }
        return children;
    }

    public static List getUserMenuTree(List<UserMenuEntity> data, String menuId) {
        if(data.size()==0){
            return null;
        }
        List<UserMenu> children = new ArrayList<>();
        for(UserMenuEntity tree:data){
            if(tree.getParentId().equals(menuId)){
                List<UserMenuEntity> childrenData = new ArrayList<>(data);
                childrenData.remove(tree);
                UserMenu userMenu = new UserMenu();
                userMenu.setPath(tree.getPath());
                userMenu.setId(tree.getMenuId());
                userMenu.setMeta(new Meta(tree.getName(),tree.getIcon()));
                userMenu.setChildren(getUserMenuTree(childrenData,tree.getMenuId()));
                children.add(userMenu);
            }
        }
        return children;
    }

}
